﻿using AspNetCoreRateLimit;
using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace apitest.Web.Core
{
    public static partial class Extention
    {
        /// <summary>
        /// IPLimit限流 启动服务
        /// </summary>
        public static IServiceCollection AddIpRateLimitSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            if (App.Configuration["IpRateLimiting:Enabled"] == "True")
            {
                services.Configure<IpRateLimitOptions>(App.Configuration.GetSection("IpRateLimiting"));

                services.AddInMemoryRateLimiting();

                services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();

            }

            return services;
        }
    }
    public static partial class Extention
    {
        public static IApplicationBuilder UseIpLimit(this IApplicationBuilder app)
        {
            if (App.Configuration["IpRateLimiting:Enabled"] == "True")
            {
                return app.UseIpRateLimiting();
            }
            return app;
        }
    }
}
